//
//  LNRefreshControl.swift
//  727-黑马08微博-测试
//
//  Created by apple on 15/8/7.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
 /// 下拉刷新的偏移量，超出之后，翻转动画
private let kRefreshPullOffset: CGFloat = -60
/// 下拉刷新控件，集成在tableView controller
class LNRefreshControl: UIRefreshControl {
//    MARK: - 重写结束刷新方法
    override func endRefreshing() {
        super.endRefreshing()
        refreshView.stopLoading()
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
//        如果y>0直接返回
        if frame.origin.y > 0{
            return
        }
//        判断是否开始刷新，如果进入刷新状态，播放加载动画
        if refreshing {
            refreshView.startLoading()
        }
        if frame.origin.y < kRefreshPullOffset && !refreshView.rotateFlag {
            print("反过来")
            refreshView.rotateFlag = true
        } else if frame.origin.y > kRefreshPullOffset && refreshView.rotateFlag {
            print("转过来")
            refreshView.rotateFlag = false
        }
        
        
        
    }
    
    
    
    
//    MARK: - 构造函数
    override init() {
        super.init()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    deinit {
        self.removeObserver(self, forKeyPath: "frame")
    }
    

    private func setupUI() {
//        KVO监听frame属性变化
        self.addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
//        隐藏默认的转轮
        tintColor = UIColor.clearColor()
        
        
//        添加刷新视图
        addSubview(refreshView)
//        自动布局 - 从xib加载的时候，可以直接取到真实的大小
        refreshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: refreshView.bounds.size)
        
    }
    
//    MARK: - 懒加载控件
    private lazy var refreshView: LNRefreshView = LNRefreshView.refreshView()
    
}

class LNRefreshView: UIView {
    
//   旋转标记
    private var rotateFlag = false {
        didSet {
            rotateTipIcon()
        }
        
    }
    
    @IBOutlet weak var loadingIcon: UIImageView!
    
    
    @IBOutlet weak var tipView: UIView!
    @IBOutlet weak var tipIcon: UIImageView!
    
/// 从xib加载刷新视图
    class func refreshView() -> LNRefreshView {
        return NSBundle.mainBundle().loadNibNamed("LNRefreshView", owner: nil, options: nil).last as! LNRefreshView
    }
/// 提示图标的旋转
    private func rotateTipIcon() {
//        在块代码动画中，旋转会就近原则，默认顺时针寻找方向
        let angle = rotateFlag ? CGFloat(M_PI - 0.01) : CGFloat(M_PI + 0.01)
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.tipIcon.transform = CGAffineTransformRotate(self.tipIcon.transform, angle)
            
        }
    }
/// 开始动画
    private func startLoading() {
//        1.判断动画是否已经被添加
        if loadingIcon.layer.animationForKey("loadingAnim") != nil {
            return
        }
//        2.隐藏提示视图
        tipView.hidden = true
//        3.定义动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 1.0
//        3.添加动画
        loadingIcon.layer.addAnimation(anim, forKey: "loadingAnim")
        
    }
/// 停止加载动画
    private func stopLoading() {
        tipView.hidden = false
        loadingIcon.layer.removeAllAnimations()
    }
    
    
}



























